package org.yufan.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.yufan.admin.service.ItemDescService;
import org.yufan.admin.service.ItemService;
import org.yufan.core.bean.Item;
import org.yufan.core.bean.ItemDesc;

import java.util.Date;

/**
 * Created by zxd on 2019/3/5
 **/
@Service
public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService {
    @Autowired
    private ItemDescService itemDescService;
    @Override
    public void insertItem(Item item, String desc) {
        item.setStatus(Item.ITEM_UN_NORMAL);
        item.setUpdated(new Date());
        item.setCreated(item.getUpdated());
        this.insert(item);

        ItemDesc itemDesc=new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setUpdated(item.getUpdated());
        itemDesc.setCreated(item.getUpdated());
        itemDescService.insert(itemDesc);
    }

    @Override
    public void updateItem(Item item, String desc) {

        item.setUpdated(new Date());
        this.update(item);

        ItemDesc itemDesc=new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setUpdated(item.getUpdated());
        itemDescService.update(itemDesc);

    }

    @Override
    public void updateReshelf(String ids) {
        if(StringUtils.isEmpty(ids)){
            return;
        }
        String[] idsArray = ids.split(",");
        if(idsArray!=null){
            for (String id : idsArray) {
                Item item = this.findById(Long.valueOf(id));
                //判断是否是下架状态
                if(item!=null&&item.getStatus()==Item.ITEM_UN_NORMAL){
                    item.setStatus(Item.ITEM_NORMAL);
                    item.setUpdated(new Date());
                    this.update(item);
                }
            }
        }
    }
}
